function e(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

Object.defineProperty(exports, "__esModule", {
    value: !0
});

var t = e(require("./api-code")), n = e(require("event-emitter")), o = {
    URL: "wss://socket.ibos.cn",
    EVENT_PREFIX: "socket",
    EVENT_TYPES: {
        MESSAGE_CHAT: "event.chat",
        MESSAGE_NOTIFY: "event.messagenotify",
        DATE_VOICE: "event.activity_create_voice_tips"
    },
    connect: function(e) {
        var o = this;
        this.connected || (this.accesstoken = e, wx.connectSocket({
            url: this.URL,
            header: {
                "content-type": "application/json"
            },
            method: "GET"
        }), wx.onSocketOpen(function(e) {
            console.log("socket 连接成功", e), o.connected = !0, o.send({
                cmd: "api",
                route: "weapp/login",
                params: {
                    accesstoken: o.accesstoken
                }
            });
        }), wx.onSocketError(function(e) {
            console.error("socket 连接失败", e), o.connected = !1, o.reconnect();
        }), wx.onSocketClose(function(e) {
            o.connected = !1, e && e.code === t.default.SOCKET_AUTO_CLOSE ? (console.error("socket 自动关闭", e), 
            o.reconnect()) : console.error("socket 连接关闭", e);
        }), wx.onSocketMessage(function(e) {
            var t = e.data;
            try {
                t = JSON.parse(t);
            } catch (e) {}
            t.event ? n.default.emit(o.EVENT_PREFIX + ".event." + t.event, t) : t.cmd && n.default.emit(o.EVENT_PREFIX + ".cmd." + t.cmd, t), 
            console.log("socket 收到服务器内容：", t);
        }));
    },
    reconnect: function() {
        this.accesstoken && (console.log("socket 重新连接"), this.connect(this.accesstoken));
    },
    send: function(e) {
        return new Promise(function(t, n) {
            wx.sendSocketMessage({
                data: JSON.stringify(e),
                success: function(e) {
                    return t(e);
                },
                fail: function(e) {
                    return n(e);
                }
            });
        });
    },
    on: function(e, t) {
        var o = [ this.EVENT_PREFIX + "." + e, t ];
        return n.default.addListener.apply(n.default, o), function() {
            n.default.removeListener.apply(n.default, o);
        };
    },
    once: function(e, t) {
        var n = this.on(e, function(e) {
            t && t(e), n();
        });
        return n;
    },
    close: function() {
        wx.closeSocket(), n.default.removeListener(this.EVENT_PREFIX + "." + this.EVENT_TYPES.MESSAGE_CHAT);
    }
};

exports.default = o;